import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';

class CodeScreen extends StatefulWidget {
  static const routeName = "/code";

  @override
  _CodeScreenState createState() => _CodeScreenState();
}

class _CodeScreenState extends State<CodeScreen> {
  String code = "";

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> arguments = ModalRoute.of(context).settings.arguments;
    String fileName = arguments['file'];
    _readFileContent(fileName).then((value) {
      setState(() {
        code = value;
      });
    });
    return Scaffold(
      appBar: AppBar(
        title: Text('$fileName'),
      ),
      body: SingleChildScrollView(
        child: MarkdownBody(
          data: '$code',
          styleSheetTheme: MarkdownStyleSheetBaseTheme.cupertino,
        ),
      ),
    );
  }

  // 获取本地文件
  Future<String> _readFileContent(String fileName) async {
    return await rootBundle.loadString('assets/code/$fileName.md');
  }
}
